//
//  common.cc
//  Draco
//
//  Created by Liu Dafan on 12-3-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#include "common.h"
#include "base.h"

Config* readconf(const char *file)
{
    char buf[1024];
    
    Config *cf = (Config *)malloc(sizeof(Config));
    if (cf == NULL) {
		DERROR("malloc error\n");
        return NULL;
    }
    
    memset(cf, 0, sizeof(*cf));
    strcpy(cf->conf, realpath(file, buf));
    DINFO("config file: %s\n", cf->conf);
    
    ConfParser * cf_p = confparser_create(file);
    
    //confparser_add_param(cf_p, cf, "valid_host", CONF_USER, 0, (void *)parser_host);
    confparser_add_param(cf_p, &cf->hbase_host, "hbase_host", CONF_STRING, 0, NULL);
    confparser_add_param(cf_p, &cf->hbase_port, "hbase_port", CONF_INT, 0, NULL);
    confparser_add_param(cf_p, &cf->hdfs_host, "hdfs_host", CONF_STRING, 0, NULL);
    confparser_add_param(cf_p, &cf->hdfs_port, "hdfs_port", CONF_INT, 0, NULL);
    confparser_add_param(cf_p, &cf->memlink_host, "memlink_host", CONF_STRING, 0, NULL);
    confparser_add_param(cf_p, &cf->memlink_read_port, "memlink_read_port", CONF_INT, 0, NULL);
    confparser_add_param(cf_p, &cf->memlink_write_port, "memlink_write_port", CONF_INT, 0, NULL);
    confparser_add_param(cf_p, &cf->connect_num, "connect_num", CONF_INT, 0, NULL);
    confparser_add_param(cf_p, &cf->rootdir, "root_dir", CONF_STRING, 0, NULL);
    
    if (confparser_parse(cf_p) < 0) {
		free(cf);
        return NULL;
    }
    
	DINFO("hbase_host: %s\n", cf->hbase_host);
	DINFO("hbase_port: %d\n", cf->hbase_port);
	DINFO("hdfs_host: %s\n", cf->hdfs_host);
	DINFO("hdfs_port: %d\n", cf->hdfs_port);
	DINFO("memlink_host: %s\n", cf->memlink_host);
	DINFO("memlink_read_port: %d\n", cf->memlink_read_port);
	DINFO("memlink_write_port: %d\n", cf->memlink_write_port);
	DINFO("connect_num: %d\n", cf->connect_num);
    DINFO("root_dir: %s\n", cf->rootdir);
    
	return cf;
}

